package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.org.domain.Department;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.query.DepartmentQuery;
import cn.itsource.pethome.org.service.IDepartmentService;
import cn.itsource.pethome.org.service.IEmployeeService;
import cn.itsource.pethome.basic.util.AjaxResult;
import cn.itsource.pethome.basic.util.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
@Api(tags = "部门管理所有的接口",description = "部门接口描述")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IEmployeeService employeeService;

    @PostMapping// 相当于@RequestMapping(method = RequestMethod.POST)
    @Transactional
    @ApiOperation(value = "添加或修改部门",notes = "有id为修改，无id为添加")
    public AjaxResult sava(@RequestBody Department department){
        try {
            departmentService.save(department);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后再试！！！");
        }
    }

    @DeleteMapping("/{id}")// 相当于@RequestMapping(method = RequestMethod.POST)
    @Transactional
    @ApiOperation(value = "根据ID删除部门")
    public AjaxResult delete(@PathVariable("id") Long id){//@PathVariable必须写
        try {
            departmentService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后再试！！！");
        }
    }

    @GetMapping("/{id}")// 相当于@RequestMapping(method = RequestMethod.GET)
    @ApiOperation(value = "根据ID查询部门")
    public Department findById(@PathVariable("id") Long id){
        return departmentService.findById(id);
    }

    @PatchMapping// 相当于@RequestMapping(method = RequestMethod.PATCH)
    @ApiOperation(value = "查询所有的部门",notes = "我们这个接口没有分页哦")
    public PageInfo<Department> findPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.findPage(departmentQuery);
    }

    @PatchMapping("/batchRemove")
    @Transactional
    @ApiOperation(value = "根据IDs删除部门")
    public AjaxResult batchRemove(@RequestBody List<Department> departments){
        try {
            System.out.println(departments.toString());
            departmentService.deleteMany(departments);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后再试！！！");
        }
    }

    /**
     * 查询所有员工
     * @param
     * @return
     */
    @PatchMapping("/findManager")// 相当于@RequestMapping(method = RequestMethod.PATCH)
    public List<Employee> findManager(){
        return employeeService.findAll();
    }

    /**
     * 查询所有部门树
     * @return
     */
    @PatchMapping("/findDepartmentTree")// 相当于@RequestMapping(method = RequestMethod.PATCH)
    public List<Department> findDepartmentTree(){
        return departmentService.findDepartmentTree();
    }
}
